import CryptoJS from 'crypto-js'

/**
 * @word 要加密的内容
 * @keyWord String  服务器随机返回的关键字
 */
export function aesEncrypt(word: string, keyWord: string = 'XwKsGlMcdPMEhR1B'): string {
  const key = CryptoJS.enc.Utf8.parse(keyWord)
  const srcs = CryptoJS.enc.Utf8.parse(word)
  const encrypted = CryptoJS.AES.encrypt(srcs, key, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7,
  })
  return encrypted.toString()
}

/**
 * 重新设置验证组件尺寸
 */
export function resetSize(instance: any) {
  let imgSize = Object.assign({ width: '310px', height: '155px' }, instance.imgSize || {})
  let barSize = Object.assign({ width: '310px', height: '40px' }, instance.barSize || {})

  const setBarWidth = parseInt(imgSize.width)
  const setBarHeight = parseInt(barSize.height)
  const setImgWidth = parseInt(imgSize.width)
  const setImgHeight = parseInt(imgSize.height)

  return {
    imgWidth: setImgWidth + 'px',
    imgHeight: setImgHeight + 'px',
    barWidth: setBarWidth + 'px',
    barHeight: setBarHeight + 'px',
  }
}

/**
 * 生成唯一ID
 */
export function generateUuid(): string {
  const s: string[] = []
  const hexDigits = '0123456789abcdef'
  for (let i = 0; i < 36; i++) {
    s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1)
  }
  s[14] = '4' // bits 12-15 of the time_hi_and_version field to 0010
  s[19] = hexDigits.substr((s[19] as any & 0x3) | 0x8, 1) // bits 6-7 of the clock_seq_hi-and_reserved to 01
  s[8] = s[13] = s[18] = s[23] = '-'

  return s.join('')
}
